<<<<<<< HEAD
package com.hsx.servlet;

import com.hsx.pojo.Person;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class SessionDemo01  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决控制台乱码问题
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //得到Session
        HttpSession session = req.getSession();
        Person person = new Person("hsx", 20);
        session.setAttribute("name",person);

        //得到Session 的Id
        PrintWriter out = resp.getWriter();
        String id = session.getId();
        if (session.isNew()) {
            out.print("Session不存在，您的ID为"+id);
        }else {
            out.print("Session存在了，且id为"+id);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
=======
package com.hsx.servlet;

import com.hsx.pojo.Person;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class SessionDemo01  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决控制台乱码问题
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //得到Session
        HttpSession session = req.getSession();
        Person person = new Person("hsx", 20);
        session.setAttribute("name",person);

        //得到Session 的Id
        PrintWriter out = resp.getWriter();
        String id = session.getId();
        if (session.isNew()) {
            out.print("Session不存在，您的ID为"+id);
        }else {
            out.print("Session存在了，且id为"+id);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
>>>>>>> 0955e43271ec003176a75da3f24c6d9c0071fe9b
